﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Media.Animation;
using System.Diagnostics;
using Microsoft.VisualBasic.ApplicationServices;

namespace WpfApp9
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        
        public  int speed = 0; //用于变速(仅在马里奥死亡的时候)
        private bool isBullet = false;
        private bool isKeyUp = false;
        private int[,] map = Read2DArrayFromFile("..\\..\\..\\..\\..\\MYMAP.map");
        private LinkedList<Monster> monsterList = new LinkedList<Monster>();//monster链表
        private LinkedList<Mushroom> mushroomList = new LinkedList<Mushroom>();
        Dictionary<int, Rectangle> coinDictionary = new Dictionary<int, Rectangle>();
        private Mario mario;                //玩家角色，见Mario.cs
        private Ellipse marioDisplayCtrl;   //代表玩家的圆形显示控件
        private Ellipse bulletDisplayCtrl;  //子弹
        private DispatcherTimer timer;      //主时间片定时器
        //MediaElement bgm = new MediaElement();
        private Bullet bullet =  new Bullet(50, 21);
        ImageBrush imageBrushmario = new ImageBrush();
        ImageBrush imageBrushmonster = new ImageBrush();
        //private double speed = 0.1; //怪物移动速度
        double i = 0;
        int points; 
        public MainWindow()
        {
            InitializeComponent();

            //初始化主时间片
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(20); //25FPS
            timer.Tick += Timer_Tick;
            timer.Start();

            //首次绘制场景
            //BGM
           
            bgm.Source = new Uri("..\\..\\..\\..\\..\\sound\\地上.mp3", UriKind.Relative);
            // 设置音量，0为静音，1为最大
            bgm.Volume = 0.5;
            // 设置加载行为为手动，这样可以在代码中控制播放
            bgm.LoadedBehavior = MediaState.Manual;
            // 设置循环模式为无限循环
            bgm.MediaEnded += (s, e) => bgm.Position = TimeSpan.Zero;
            // 将MediaElement控件添加到主窗口中
            //this.Content = bgm;
            // 开始播放背景音乐
            bgm.Play();

            //初始化测试数据
            mario = new Mario();
           
            DrawMap();
            DrawMario();
           


        }

        //此函数每秒调用40次，所有逻辑均在此完成
        private void Timer_Tick(object? sender, EventArgs e)
        {
            //处理操作
            if (mario.Lives == 0)
            {
                mario.Die();
                
                //// 停止主时间片定时器
                //timer.Stop(); // 播放游戏结束音效
                //bgm.Stop();
                //MusicPlay("游戏结束"); // 弹出游戏结束对话框，询问是否重新开始或退出游戏
                //if (MessageBox.Show("游戏结束，是否重开游戏？", "提示", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                //{
                //    string filePath = Process.GetCurrentProcess().MainModule.FileName;
                //    // 启动一个新的进程，以重新启动当前应用程序
                //    Process.Start(filePath);

                //    // 关闭当前应用程序
                //    Environment.Exit(0);
                //}
                //else
                //{
                //    // 关闭当前应用程序
                //    Environment.Exit(0);
                //}
                
            }

            else {
                if (mario.HeightToJump == 0)
                {
                    if (mario.DirectionofMario == Direction.Right)
                    {
                        imageBrushmario.ImageSource = new BitmapImage(new Uri("..\\..\\..\\..\\..\\pic\\mario.png", UriKind.Relative)); // 将 "path_to_your_image.png" 替换为你的图片路径
                        marioDisplayCtrl.Fill = imageBrushmario;
                    }
                    else if (mario.DirectionofMario == Direction.Left)
                    {
                        imageBrushmario.ImageSource = new BitmapImage(new Uri("..\\..\\..\\..\\..\\pic\\marioleft.png", UriKind.Relative)); // 将 "path_to_your_image.png" 替换为你的图片路径
                        marioDisplayCtrl.Fill = imageBrushmario;
                    }
                }
                else
                {
                    if (mario.DirectionofMario == Direction.Right)
                    {
                        imageBrushmario.ImageSource = new BitmapImage(new Uri("..\\..\\..\\..\\..\\pic\\mariojump.png", UriKind.Relative)); // 将 "path_to_your_image.png" 替换为你的图片路径
                        marioDisplayCtrl.Fill = imageBrushmario;
                    }
                    else if (mario.DirectionofMario == Direction.Left)
                    {
                        imageBrushmario.ImageSource = new BitmapImage(new Uri("..\\..\\..\\..\\..\\pic\\mariojumpleft.png", UriKind.Relative)); // 将 "path_to_your_image.png" 替换为你的图片路径
                        marioDisplayCtrl.Fill = imageBrushmario;
                    }
                }
                if (Keyboard.IsKeyDown(Key.Left))
                {
                    if (map[(int)(mario.Y+0.99f), (int)(mario.X+i - 0.01f)] != 1)
                    {
                        mario.SetDir(Direction.Left);
                        imageBrushmario.ImageSource = new BitmapImage(new Uri("..\\..\\..\\..\\..\\pic\\mariomoveleft.png", UriKind.Relative)); // 将 "path_to_your_image.png" 替换为你的图片路径
                        marioDisplayCtrl.Fill = imageBrushmario;
                        i -= 0.25;
                        Canvas.SetLeft(childCanvas, -i*25);
                    }
                }
                if (Keyboard.IsKeyDown(Key.Right))
                {
                    if (map[(int)(mario.Y+0.99f), (int)(mario.X+i ) + 1] != 1)
                    {
                        mario.SetDir(Direction.Right);
                        i += 0.25;
                        Canvas.SetLeft(childCanvas, -i*25);
                        imageBrushmario.ImageSource = new BitmapImage(new Uri("..\\..\\..\\..\\..\\pic\\mariomove.png", UriKind.Relative)); // 将 "path_to_your_image.png" 替换为你的图片路径
                        marioDisplayCtrl.Fill = imageBrushmario;
                    
                    }
                

                }
                if (Keyboard.IsKeyUp(Key.Up))
                {
                    isKeyUp = true;
                }
                if (isKeyUp && Keyboard.IsKeyDown(Key.Up))
                {                 
                    isKeyUp = false;
                    if(mario.DirectionofMario == Direction.Right)
                    {
                        imageBrushmario.ImageSource = new BitmapImage(new Uri("..\\..\\..\\..\\..\\pic\\mariojump.png", UriKind.Relative)); // 将 "path_to_your_image.png" 替换为你的图片路径
                        marioDisplayCtrl.Fill = imageBrushmario;
                    }
                    else if (mario.DirectionofMario == Direction.Left)
                    {
                        imageBrushmario.ImageSource = new BitmapImage(new Uri("..\\..\\..\\..\\..\\pic\\mariojumpleft.png", UriKind.Relative)); // 将 "path_to_your_image.png" 替换为你的图片路径
                        marioDisplayCtrl.Fill = imageBrushmario;
                    }
                    if (mario.HeightToJump == 0 && (map[(int)mario.Y + 1, (int)(mario.X+i + 0.99f)]!=0 || map[(int)mario.Y + 1, (int)(mario.X+i)] != 0))//为了处理左右边缘起跳问题
                    {
                        MusicPlay("跳");
                        mario.HeightToJump = 6.5;
                    
                    }
                }
                if (Keyboard.IsKeyDown(Key.Space))
                {
                    if(isBullet == false) { 
                        bullet.X = mario.X;
                        bullet.Y = mario.Y;
                        bullet.setDirectionofBullet(mario.DirectionofMario);
                        isBullet = true;
                    }

                }

                // 如果子弹已发射，更新子弹位置
                if (isBullet)
               {
                   bullet.Move();
                   DrawBullet();
                    // 如果子弹飞出屏幕
                    if (map[(int)(bullet.Y), (int)(bullet.X+i )] == 1)
                    {
                        bullet.X = 50;
                        DrawBullet();
                        isBullet = false;
                    }
                   else if (bullet.IsOutOfScreen())
                   {
                        // 标记子弹未发射
                        isBullet = false;
                   }
               }
                //更新mario的状态
                if (mario.HeightToJump > 0 && map[(int)(mario.Y-0.01f), (int)(mario.X +i +0.99f)] != 1 && map[(int)(mario.Y - 0.01f), (int)(mario.X+i)] != 1 &&(int)mario.Y > 0 && map[(int)(mario.Y - 0.01f), (int)(mario.X + i + 0.99f)] != 4 && map[(int)(mario.Y - 0.01f), (int)(mario.X + i)] != 5 && map[(int)(mario.Y - 0.01f), (int)(mario.X + i + 0.99f)] != 1 && map[(int)(mario.Y - 0.01f), (int)(mario.X + i)] != 5)
                {   //这里的0.1f是为了解决跳上去撞到墙可进去一点的问题
                
                    mario.Y -= 0.25;
                    mario.HeightToJump -= 0.25;
                }
                else
                {
                    mario.HeightToJump = 0;
                    if(map[(int)(mario.Y-1),(int)(mario.X+i)] == 4)
                    {
                        map[(int)(mario.Y - 1), (int)(mario.X + i)] = 5;
                        MusicPlay("顶出蘑菇,花或星");
                        CreatMushroom();
                    }
                    //下落判断
                    if (map[(int)(mario.Y + 1), (int)(mario.X +i+ 0.99f)] != 1 && map[(int)mario.Y + 1, (int)(mario.X+i)] != 1 && map[(int)(mario.Y + 1), (int)(mario.X + i + 0.99f)] != 4 && map[(int)mario.Y + 1, (int)(mario.X + i)] != 4 && map[(int)(mario.Y + 1), (int)(mario.X + i + 0.99f)] != 5 && map[(int)mario.Y + 1, (int)(mario.X + i)] != 5)
                    {
                        mario.Y += 0.25;
                    }


                }

                
                //重绘
                //暂时只绘制角色
                getCoin();
                DrawMushroom();
                UpdateMonsterState();
                DrawMonster();
            }
            DrawMario(); 
            Gameover(mario.Y);
            Gamewin();
               
            
            

        }
        /// <summary>
        /// 绘制场景，注意只有有墙的位置才创建对应的rect
        /// </summary>
        private void DrawMap()
        {
            for (int i = 0; i < 24; i++)
            {
                for (int j = 0; j < 200; j++)
                {
                    if (map[i, j] == 1)
                    {
                        Rectangle rect = new Rectangle();
                        rect.Fill = new SolidColorBrush(Colors.Gray);
                        ImageBrush imageBrush = new ImageBrush();
                        imageBrush.ImageSource = new BitmapImage(new Uri("..\\..\\..\\..\\..\\pic\\block.png", UriKind.Relative)); // 将 "path_to_your_image.png" 替换为你的图片路径

                        rect.Fill = imageBrush;
                        rect.Width = 24;
                        rect.Height = 24;
                        rect.SetValue(Canvas.LeftProperty, j * 25.0);
                        rect.SetValue(Canvas.TopProperty, i * 25.0);
                        childCanvas.Children.Add(rect);

                    }
                    else if (map[i, j] == 2)
                    {
                        Monster monster = new Monster();
                        monster.state = true;//活着
                        monster.X = j;
                        monster.Y = i;
                        monster.speed = 0.1;
                        Rectangle monsterDisplayCtrl = new Rectangle();
                        monsterDisplayCtrl.Fill = new SolidColorBrush(Colors.Red);
                        ImageBrush imageBrush = new ImageBrush();
                        imageBrush.ImageSource = new BitmapImage(new Uri("..\\..\\..\\..\\..\\pic\\enemy1.png", UriKind.Relative)); // 将 "path_to_your_image.png" 替换为你的图片路径
                        monsterDisplayCtrl.Fill = imageBrush;
                        monsterDisplayCtrl.Width = 24;
                        monsterDisplayCtrl.Height = 24;
                        monsterDisplayCtrl.SetValue(Canvas.LeftProperty, monster.X * 25.0);
                        monsterDisplayCtrl.SetValue(Canvas.TopProperty, monster.Y * 25.0);
                        childCanvas.Children.Add(monsterDisplayCtrl);
                        monster.displayCtrl = monsterDisplayCtrl;
                        monsterList.AddLast(monster);
                    }
                    else if (map[i, j] == 3)
                    {
                        Rectangle coin = new Rectangle();
                        coin.Fill = new SolidColorBrush(Colors.Gray);
                        ImageBrush imageBrush = new ImageBrush();
                        imageBrush.ImageSource = new BitmapImage(new Uri("..\\..\\..\\..\\..\\pic\\coin.png", UriKind.Relative)); // 将 "path_to_your_image.png" 替换为你的图片路径
                        coin.Fill = imageBrush;
                        coin.Width = 24;
                        coin.Height = 24;
                        coin.SetValue(Canvas.LeftProperty, j * 25.0);
                        coin.SetValue(Canvas.TopProperty, i * 25.0);
                        childCanvas.Children.Add(coin);
                        coinDictionary.Add(j, coin);

                    }
                    else if (map[i, j] == 4)
                    {
                        //CreatMushroom();
                        Rectangle questionBlock = new Rectangle();
                        questionBlock.Fill = new SolidColorBrush(Colors.Gray);
                        ImageBrush imageBrush = new ImageBrush();
                        imageBrush.ImageSource = new BitmapImage(new Uri("..\\..\\..\\..\\..\\pic\\question.png", UriKind.Relative)); // 将 "path_to_your_image.png" 替换为你的图片路径
                        questionBlock.Fill = imageBrush;
                        questionBlock.Width = 24;
                        questionBlock.Height = 24;
                        questionBlock.SetValue(Canvas.LeftProperty, j * 25.0);
                        questionBlock.SetValue(Canvas.TopProperty, i * 25.0);
                        childCanvas.Children.Add(questionBlock);
                        
                    }
                    // 新增的代码，绘制问号方块类型2
                    else if (map[i, j] == 5)
                    {
                        //Rectangle questionBlock2 = new Rectangle();
                        //questionBlock2.Fill = new SolidColorBrush(Colors.Gray);
                        //ImageBrush imageBrush = new ImageBrush();
                        //imageBrush.ImageSource = new BitmapImage(new Uri("..\\..\\..\\..\\..\\pic\\question2.png", UriKind.Relative)); // 将 "path_to_your_image.png" 替换为你的图片路径
                        //questionBlock2.Fill = imageBrush;
                        //questionBlock2.Width = 24;
                        //questionBlock2.Height = 24;
                        //questionBlock2.SetValue(Canvas.LeftProperty, j * 25.0);
                        //questionBlock2.SetValue(Canvas.TopProperty, i * 25.0);
                        //childCanvas.Children.Add(questionBlock2);
                    }
                    else if (map[i, j] == 6)
                    {
                        Mushroom mushroom = new Mushroom();
                        mushroom.X = j;
                        mushroom.Y = i;
                        mushroom.speed = 0.1;
                        mushroom.direction = Direction.Right;
                        Rectangle mushroomDisplayCtrl = new Rectangle();
                        mushroomDisplayCtrl.Fill = new SolidColorBrush(Colors.Red);
                        ImageBrush imageBrush = new ImageBrush();
                        imageBrush.ImageSource = new BitmapImage(new Uri("..\\..\\..\\..\\..\\pic\\mushroom.png", UriKind.Relative));
                        mushroomDisplayCtrl.Fill = imageBrush;
                        mushroomDisplayCtrl.Width = 24;
                        mushroomDisplayCtrl.Height = 24;
                        mushroomDisplayCtrl.SetValue(Canvas.LeftProperty, mushroom.X * 25.0);
                        mushroomDisplayCtrl.SetValue(Canvas.TopProperty, mushroom.Y * 25.0);
                        childCanvas.Children.Add(mushroomDisplayCtrl);
                        mushroom.displayCtrl = mushroomDisplayCtrl;
                        mushroomList.AddLast(mushroom);
                    }
                    else if (map[i, j] == 7)
                    {
                        Rectangle Castle = new Rectangle();
                        Castle.Fill = new SolidColorBrush(Colors.Gray);
                        ImageBrush imageBrush = new ImageBrush();
                        imageBrush.ImageSource = new BitmapImage(new Uri("..\\..\\..\\..\\..\\pic\\castle.png", UriKind.Relative)); // 将 "path_to_your_image.png" 替换为你的图片路径
                        Castle.Fill = imageBrush;
                        Castle.Width = 240;
                        Castle.Height = 240;
                        Castle.SetValue(Canvas.LeftProperty, j * 25.0);
                        Castle.SetValue(Canvas.TopProperty, i * 25.0);
                        childCanvas.Children.Add(Castle);
                    }
                }
            }
        }

        private void CreatMushroom()
        {
            Mushroom mushroom = new Mushroom();
            mushroom.X = mario.X+i;
            mushroom.Y = (int)mario.Y-2.5;
            mushroom.speed = 0.05;
            mushroom.direction = Direction.Right;
            // 创建一个蘑菇的显示控件
            Rectangle mushroomDisplayCtrl = new Rectangle();
            mushroomDisplayCtrl.Fill = new SolidColorBrush(Colors.Red);
            ImageBrush imageBrush_m = new ImageBrush();
            imageBrush_m.ImageSource = new BitmapImage(new Uri("..\\..\\..\\..\\..\\pic\\mushroom.png", UriKind.Relative)); // 将 "path_to_your_image.png" 替换为你的图片路径
            mushroomDisplayCtrl.Fill = imageBrush_m;
            mushroomDisplayCtrl.Width = 24;
            mushroomDisplayCtrl.Height = 24;
            mushroomDisplayCtrl.SetValue(Canvas.LeftProperty, mushroom.X * 25.0);
            mushroomDisplayCtrl.SetValue(Canvas.TopProperty, mushroom.Y * 25.0);
            childCanvas.Children.Add(mushroomDisplayCtrl);
            // 设置蘑菇的显示控件属性
            mushroom.displayCtrl = mushroomDisplayCtrl;
            // 将蘑菇加入到蘑菇链表中
            mushroomList.AddLast(mushroom);
        }
        private void DrawMushroom()
        {
            // 新增的代码，更新蘑菇链表中每个蘑菇的位置和状态
            LinkedListNode<Mushroom> currentMushroomNode = mushroomList.First;
            while (currentMushroomNode != null)
            {
                currentMushroomNode.Value.displayCtrl.SetValue(Canvas.LeftProperty, currentMushroomNode.Value.X * 25.0);
                currentMushroomNode.Value.displayCtrl.SetValue(Canvas.TopProperty, currentMushroomNode.Value.Y * 25.0);
                // 更新位置
                currentMushroomNode.Value.X += currentMushroomNode.Value.speed;
                // 如果碰到墙壁，反向移动
                if (map[(int)currentMushroomNode.Value.Y, (int)currentMushroomNode.Value.X + 1] == 1 || map[(int)currentMushroomNode.Value.Y, (int)currentMushroomNode.Value.X] == 1)
                {
                    currentMushroomNode.Value.speed = -currentMushroomNode.Value.speed;
                }
                // 如果没有地面，下落
                if (map[(int)currentMushroomNode.Value.Y + 1, (int)currentMushroomNode.Value.X] != 1&& map[(int)currentMushroomNode.Value.Y + 1, (int)currentMushroomNode.Value.X] != 5)
                {
                    currentMushroomNode.Value.Y += 0.25;
                }
                // 如果碰到马里奥，给马里奥增加一些奖励或者效果，并移除该蘑菇
                if (Math.Abs((mario.X + i) - currentMushroomNode.Value.X) < 0.8 && mario.Y == currentMushroomNode.Value.Y)
                {
                    //MusicPlay("吃到蘑菇或花");
                    
                    mario.Lives+=1; 
                    childCanvas.Children.Remove(currentMushroomNode.Value.displayCtrl);
                    LinkedListNode<Mushroom> temp = currentMushroomNode.Next;
                    mushroomList.Remove(currentMushroomNode);
                    currentMushroomNode = temp;
                    points += 1000;
                    MusicPlay("加一条命");
                   
                }
                else
                {
                    currentMushroomNode = currentMushroomNode.Next;
                }
            }
        }
        //绘制/重绘
        private void DrawBullet()
        {
            if (bulletDisplayCtrl == null)
            {
                bulletDisplayCtrl = new Ellipse();
                bulletDisplayCtrl.Fill = new SolidColorBrush(Colors.Red);
                bulletDisplayCtrl.Width =10;
                bulletDisplayCtrl.Height = 10;
                marioCanvas.Children.Add(bulletDisplayCtrl);
            }
            //更新位置
            bulletDisplayCtrl.SetValue(Canvas.LeftProperty, bullet.X * 25 );
            bulletDisplayCtrl.SetValue(Canvas.TopProperty, bullet.Y * 25);

        }
        private void DrawMario()
        {
            //仅首次，初始化
            if (marioDisplayCtrl == null)
            {
                marioDisplayCtrl = new Ellipse();
                marioDisplayCtrl.Fill = new SolidColorBrush(Colors.Red);
                marioDisplayCtrl.Width = 24;
                marioDisplayCtrl.Height = 24;
                marioCanvas.Children.Add(marioDisplayCtrl);
                imageBrushmario.ImageSource = new BitmapImage(new Uri("..\\..\\..\\..\\..\\pic\\mario.png", UriKind.Relative)); // 将 "path_to_your_image.png" 替换为你的图片路径
                marioDisplayCtrl.Fill = imageBrushmario;

            }

            //更新位置
            marioDisplayCtrl.SetValue(Canvas.LeftProperty, mario.X * 25.0);
            marioDisplayCtrl.SetValue(Canvas.TopProperty, mario.Y * 25.0);
            
           
        }

        private void DrawMonster()
        {
            LinkedListNode<Monster> currentNode = monsterList.First;
            while (currentNode != null )
            {
                if (currentNode.Value.state) { 
                    currentNode.Value.displayCtrl.SetValue(Canvas.LeftProperty, currentNode.Value.X * 25.0);
                    currentNode.Value.displayCtrl.SetValue(Canvas.TopProperty, currentNode.Value.Y * 25.0);
                    //更新位置
                }
                else
                {
 
                }
                currentNode = currentNode.Next;

            }

        }
        private void getCoin()
        {
            if (map[(int)(mario.Y), (int)(mario.X+i)] == 3 )
            {
                coinDictionary[(int)(mario.X+i)].SetValue(Canvas.LeftProperty, 50 * 25.0);
                coinDictionary[(int)(mario.X+i)].SetValue(Canvas.TopProperty, 20 * 25.0);
                map[(int)mario.Y, (int)(mario.X + i)] = 0;
                mario.Coins++;
                points += 50;
                MusicPlay("金币");
                
                
                
            }
            else if (map[(int)(mario.Y), (int)(mario.X + i + 1)] == 3)
            {
                coinDictionary[(int)(mario.X + i + 1)].SetValue(Canvas.LeftProperty, 50 * 25.0);
                coinDictionary[(int)(mario.X + i + 1)].SetValue(Canvas.TopProperty, 20 * 25.0);
                map[(int)mario.Y, (int)(mario.X + i + 1)] = 0;
                mario.Coins++;
                points += 50;
                MusicPlay("金币");
            }
            Coins框.Content = "Coins:" + mario.Coins;
            Points框.Content = "Points:" + points;
        }
        private void UpdateMonsterState()
        {
            LinkedListNode<Monster> currentNode = monsterList.First;
            while (currentNode != null)
            {
                //更新位置

                currentNode.Value.X += currentNode.Value.speed;
                if ((currentNode.Value.Y - mario.Y ==1 && mario.HeightToJump==0) && Math.Abs((mario.X+i) - currentNode.Value.X)<1)
                {
                    MonsterDie(currentNode);
                    break;
                }
                else if((Math.Abs((bullet.X + i) - currentNode.Value.X) < 1) && bullet.Y == currentNode.Value.Y)
                {
                    bullet.X = 50;
                    isBullet = false;
                    DrawBullet();
                    MonsterDie(currentNode);
                    break;
                }
                if (map[(int)currentNode.Value.Y, (int)currentNode.Value.X + 1] == 1 || map[(int)currentNode.Value.Y, (int)currentNode.Value.X] == 1)
                {
                    currentNode.Value.speed = -currentNode.Value.speed;
                }
                else if (map[(int)currentNode.Value.Y + 1, (int)currentNode.Value.X] != 1)
                {
                    currentNode.Value.Y += 0.25;
                }
                else if(Math.Abs((mario.X + i) - currentNode.Value.X) < 0.8 && mario.Y >= currentNode.Value.Y)
                {
                    mario.Lives--;//马里奥生命-1
                                  // 如果马里奥生命值为零，游戏结束
                    
                    if (mario.Lives != 0) 
                    {
                        MusicPlay("死亡1");
                        mario.Y = 11;
                    }

                    
                }
                currentNode = currentNode.Next;


            }

        }
        private void MonsterDie(LinkedListNode<Monster> currentNode)
        {
            MusicPlay("踩敌人");
            currentNode.Value.state = false;
            currentNode.Value.speed = 0;
            //currentNode.Value.X = 50;
            //currentNode.Value.Y = 20;
            currentNode.Value.X = 50;
            currentNode.Value.Y = 20;
            currentNode.Value.displayCtrl.SetValue(Canvas.LeftProperty, currentNode.Value.X * 25.0);
            currentNode.Value.displayCtrl.SetValue(Canvas.TopProperty, currentNode.Value.Y * 25.0);
            points += 100;
        }
        private void MusicPlay(string path)
        {
            MediaPlayer mediaPlayer = new MediaPlayer();
            path = System.IO.Path.Combine(Environment.CurrentDirectory,"..\\..\\..\\..\\..\\sound\\"+path+".mp3");
            // 设置要播放的音效文件的路径
            mediaPlayer.Open(new Uri(path));

            // 播放游戏音效
            mediaPlayer.Stop();
            mediaPlayer.Play();
        }
        public void Gameover(double y)
        {
            if (y >= 24)
            {
                MusicPlay("死亡2");
                timer.Stop();
                bgm.Stop();

                if (MessageBox.Show("游戏结束，是否重开游戏？", "提示", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    string filePath = Process.GetCurrentProcess().MainModule.FileName;

                    // 启动一个新的进程，以重新启动当前应用程序
                    Process.Start(filePath);

                    // 关闭当前应用程序
                    Environment.Exit(0);


                }
                else
                {

                    Environment.Exit(0);

                }
            }
        }
        public void Gamewin()
        {
            if (map[(int)(mario.Y), (int)(mario.X + i)] == 8)
            {
                timer.Stop();
                bgm.Stop();
                MusicPlay("游戏结束");
                if (MessageBox.Show("游戏胜利，是否重开游戏？", "提示", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    string filePath = Process.GetCurrentProcess().MainModule.FileName;

                    // 启动一个新的进程，以重新启动当前应用程序
                    Process.Start(filePath);

                    // 关闭当前应用程序
                    Environment.Exit(0);


                }
                else
                {

                    Environment.Exit(0);

                }
            }
        }
        static int[,] Read2DArrayFromFile(string filePath)
        {
            int[,] array = null;
            try
            {
                string[] lines = System.IO.File.ReadAllLines(filePath);
                int rows = lines.Length;
                int cols = lines[0].Split(',').Length;
                array = new int[rows, cols];

                for (int row = 0; row < rows; row++)
                {
                    string[] values = lines[row].Split(',');
                    for (int col = 0; col < cols; col++)
                    {
                        if (int.TryParse(values[col], out int val))
                        {
                            array[row, col] = val;
                        }
                        else
                        {
                            array[row, col] = 0;  // 将无效值设为0
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"读取文件出错：{ex.Message}");
                array = null;  // 发生异常时，将array设为null表示读取失败
            }

            return array;
        }
    }

}   
    


